#include<bits/stdc++.h>
using namespace std;
int n,m;
char s[2000];
bool ishuiwen(){
	int f=true;
	for(int i=0,j=strlen(s)-1;i<strlen(s),j>=0;i++,j--){
		if(s[i]!=s[j]){
			f=false;
			break;
		}
	}
	return f;
}
struct al{
	char c;
	int a,d;
}a[2005];
bool cmp1(al x,al y){
	return x.a<y.a;
}
bool cmp2(al x,al y){
	return x.d<y.d;
}
int main(){
	freopen("hw.in","r",stdin);
	freopen("hw.out","w",stdout);
	cin>>m>>n>>s;
	for(int i=1;i<=m;i++){
		cin>>a[i].c>>a[i].a>>a[i].d;
	}
	if(ishuiwen()){
		cout<<0;
	}else if(n==2){
		sort(a+1,a+n+1,cmp1);
		int mina=a[1].a;
		sort(a+1,a+n+1,cmp2);
		int mind=a[1].d;
		int minn=min(mina,mind);
		cout<<minn;
	}else if(a[1].a==a[1].d&&a[2].a==a[2].d){
		if(s[0]==s[1]||s[1]==s[2]){
			sort(a+1,a+n+1,cmp1);
			int minn=a[1].a;
			cout<<minn;
		}else{
			
		}
	}else if(n==3){
		
	}
	return 0;
} 
